package com.ant.collect.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import com.ant.collect.pojo.Menu;
import com.ant.collect.service.IUserService;
import com.ant.collect.common.ResultCode;
import com.ant.collect.common.ResultVo;
import com.ant.collect.dto.UserDto;
import com.ant.collect.handler.AntException;
import com.ant.collect.vo.UserVo;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hzd
 * @since 2025-10-23
 */
@CrossOrigin
@Api(tags = "人员信息")
@SaCheckLogin
@RestController
@RequestMapping("/user")
public class UserController {

    //StpUtil.hasPermission(String permission)：验证是否拥有某个权限。
    //StpUtil.hasPermissionAnd(String... permissions)：验证是否同时拥有所有权限（AND 关系）。
    //StpUtil.hasPermissionOr(String... permissions)：验证是否拥有任一权限（OR 关系）。

    @Autowired
    IUserService userService;

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public ResultVo userInfo() throws AntException {
        int userId = StpUtil.getLoginIdAsInt();
        Map<String, Object> map = userService.getUserInfo(userId);
        return ResultVo.success(map);
    }


    @GetMapping("/getPermission")
    public ResultVo getPermission() throws AntException {
        int userId = StpUtil.getLoginIdAsInt();
        List<Menu> permission = userService.getPermission(userId);
        return ResultVo.success(permission);
    }


    /**
     * 修改密码
     *
     * @return
     */
    @GetMapping("/findResetPassword")
    public ResultVo findResetPassword() throws AntException {
        int userId = StpUtil.getLoginIdAsInt();
        Map<String, Object> map = userService.findResetPassword(userId);
        return ResultVo.success(map);
    }


    /**
     * 保存已分配的用户角色
     *
     * @param
     * @return
     */
    @PostMapping("/save/roles/{username}")
    public ResultVo saveRoles(@PathVariable("username") String username,
                              @RequestBody List<String> roles) {
        // 手动验证：是否拥有 "btn:page1:add" 权限
        if (!StpUtil.hasPermission("btn:user:add")) {
            return ResultVo.fail("无添加权限");
        }
        userService.saveRoles(username, roles);
        return ResultVo.success();
    }


    @ApiOperation(value = "绑定信息")
    @PostMapping("/save")
    public ResultVo save(@RequestBody UserDto dto) throws ApiException {
        if (dto.getId() == null) {
            return ResultVo.success("操作成功", userService.saveUser(dto));
        } else {
            return ResultVo.success("操作成功", userService.updateUserById(dto));
        }

    }

    @ApiOperation(value = "获取员工列表")
    @GetMapping("/queryUserList")
    @SaCheckLogin
    @SaCheckPermission(value = {"btn:user:list"}, mode = SaMode.AND)
    public ResultVo queryUserList() {
        List<UserVo> list = userService.userlist();
        return ResultVo.success("ok", list);
    }


    /**
     * 分页查询用户数据
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    @SaCheckPermission(value = {"btn:user:list"}, mode = SaMode.AND)
    public ResultVo queryUserList(UserDto dto) {
        Map<String, Object> map = userService.queryUserList(dto);
        return ResultVo.success(map);
    }


    @ApiOperation(value = "通过用户id修改信息")
    @GetMapping("/reviewUser")
    public ResultVo reviewUser(Integer id) throws ApiException {
        if (id == null) {
            throw new AntException(ResultCode.FAILURE.getResultCode(), "请输入id号");
        }
        userService.reviewUser(id);
        return ResultVo.success("操作成功");
    }

    //  @SaCheckPermission("btn:user:add") // 标注需要的权限
    @ApiOperation(value = "查询员工详情")
    @GetMapping("/queryUserOne")
    @SaCheckPermission(value = {"btn:user:add", "btn:user:edit"}, mode = SaMode.AND)
    public ResultVo queryUserOne(String id) throws ApiException {
        if (id == null) {
            throw new AntException(ResultCode.FAILURE.getResultCode(), "请输入id号");
        }
        return userService.queryUserOne(id);
    }


}
